/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.customlink;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Iterator;

import javax.swing.ImageIcon;

import twaver.AlarmSeverity;
import twaver.DataBoxSelectionEvent;
import twaver.DataBoxSelectionListener;
import twaver.Element;
import twaver.Follower;
import twaver.Link;
import twaver.ResizableNode;
import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.TNetwork;
import twaver.network.background.ColorBackground;
import demo.DemoPane;

public class CustomLinkUIDemo extends DemoPane{

	static{
		ImageIcon icon = TWaverUtil.getImageIcon("/demo/network/miscellaneous/customlink/alarm.png", Color.CYAN);
		TWaverUtil.registerImageIcon("red_alarm_icon", icon);
		TUIManager.registerAttachment("alarmIconAttachment", "red_alarm_icon", TWaverConst.POSITION_CENTER);
	}
	
	private TDataBox box = new TDataBox();
	private TNetwork network = new TNetwork(box);

	public CustomLinkUIDemo() {
		this.add(network, BorderLayout.CENTER);

		ColorBackground background = new ColorBackground(Color.black);
		background.setGradientColor(Color.cyan);
		background.setGradientFactory(TWaverConst.GRADIENT_EXTEND_S);
		background.setGradient(true);
		network.setBackground(background);
		
		ResizableNode center = new ResizableNode();
		center.setSize(30, 15);
		center.putBorderColor(Color.BLUE);
		center.putCustomDraw(true);
		center.putCustomDrawOutline(false);
		center.putCustomDrawShapeFactory(TWaverConst.SHAPE_ROUND_RECTANGLE_HALF);
		center.putCustomDrawGradientFactory(TWaverConst.GRADIENT_EXTEND_VERTICAL);
		center.getAlarmState().setNewAlarmCount(AlarmSeverity.CRITICAL, 1);
		center.putAlarmBalloonTextColor(Color.WHITE);
		center.setLocation(620, 270);
		box.addElement(center);

		int count = 50;
		Follower firstNode = null;
		Follower lastNode = null;
		for (int i = 0; i < count; i++) {
			Follower node = new Follower();
			if(lastNode == null){
				firstNode = node;
			}else{
				node.setHost(lastNode);
			}
			lastNode = node;
			node.setSize(20, 10);
			node.putBorderColor(Color.BLUE);
			node.putCustomDraw(true);
			node.putCustomDrawOutline(false);
			node.putCustomDrawShapeFactory(TWaverConst.SHAPE_ROUND_RECTANGLE);
			node.putCustomDrawFillColor(Color.YELLOW);
			int x = 200 + (int) (170 * Math.cos(Math.PI * 2 / count * i));
			int y = 200 + (int) (100 * Math.sin(Math.PI * 2 / count * i));
			node.setLocation(x, y);
			box.addElement(node);
			CustomLink link = new CustomLink(node, center);
			link.putLinkFromArrowColor(Color.RED);
			link.putLinkToArrowColor(Color.RED);
			link.putLinkFromArrowOutlineColor(Color.WHITE);
			link.putLinkToArrowOutlineColor(Color.WHITE);
			box.addElement(link);
		}
		firstNode.setHost(lastNode);
		
        // move mouse then select element.
		network.getCanvas().addMouseMotionListener(new MouseMotionAdapter() {
			public void mouseMoved(MouseEvent e) {
				Element element = network.getElementPhysicalAt(e.getPoint());
				if(element != null && !element.isSelected()){
					box.getSelectionModel().setSelection(element);
				}
			}
		});
		
		box.getSelectionModel().addDataBoxSelectionListener(new DataBoxSelectionListener(){
			public void selectionChanged(DataBoxSelectionEvent e) {
				Iterator it = e.getElements().iterator();
				while(it.hasNext()){
					Element element = (Element)it.next();
					if(element instanceof Link){
						Link link = (Link)element;
						boolean selected = link.isSelected();
						if(selected){
							link.getFrom().addAttachment("alarmIconAttachment");
							link.getTo().addAttachment("alarmIconAttachment");
						}else{
							link.getFrom().removeAttachment("alarmIconAttachment");
							link.getTo().removeAttachment("alarmIconAttachment");
						}
						link.putLinkFromArrow(selected);
						link.putLinkToArrow(selected);
						link.putLinkAntialias(selected);
						link.putLinkWidth(selected ? 3 : 0);
						link.putLinkColor(selected ? Color.WHITE : Color.GREEN);
					}
				}
			}
		});
	}

}